<?php
namespace App\Modules\Admin\Http\Controllers;
use App\Models\permission;
use App\Models\Icon;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;

class BaseController extends Controller
{

    /**
     * 处理权限分类
     * @param array $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     */
    public function permissionTree($list=[], $pk='id', $pid = 'parent_id', $child = '_child', $root = 0)
    {
        if (empty($list)){
            $list = Permission::get()->toArray();
        }
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId =  $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    /**
     * 获取列表参数
     * @param [type] $keyStatus
     * @param [type] $key
     * @method getParam
     * @author chengzhigang<1256699215@qq.com>
     * @param
     * @return
     */
    public function getParam($keyStatus,$key){
        $status = Cache::pull($keyStatus);
        $requestParam = [];
        if($status){
            $requestParam = Cache::get($key);
            if($requestParam){
                $arr = explode('&', $requestParam);//转成数组
                $requestParam = [];
                foreach ($arr as $k => $v) {
                    $arr = explode('=', $v);
                    $requestParam[$arr[0]] = $arr[1];
                }
            }else{
                $requestParam = [];
            }
        }
        $requestParam = json_encode($requestParam);
        return $requestParam;
    }

    /**
     * 富文本上传图片
     * @param Request $request
     * @method editorUpload
     * @author chengzhigang<1256699215@qq.com>
     */
    public function editorUpload(Request $request){
        $file = $request->file('imgFile');
        $dir = 'uploads/editor';
        $result = upload($file,'image',['dir'=>$dir]);
        if($result['status']==1){
            exit(json_encode(array('error' => 0, 'url' => $result['data'])));
        }else{
            exit(json_encode(array('error' => 1, 'message' => $result['msg'])));
        }
    }

    /**
     * 图标列表
     * @param Request $request
     * @method icons
     * @author chengzhigang<1256699215@qq.com>
     */
    public function icons(Request $request){
        $filter = $request->input('filter');
        $icons = Icon::where(function($query) use ($filter){
            $filter && $query->where('name','like','%'.trim($filter).'%')->orWhere('desc','like','%'.trim($filter).'%');
        })->get();
        return view('admin::permission.icon',compact('icons','filter'));
    }

    /**
     * 用户列表
     * @param Request $request
     * @method userList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function userList(Request $request){
        $filter = $request->input('filter');
        $users = User::where(function($query) use ($filter){
            $filter && $query->where('nickname','like','%'.trim($filter).'%')->orWhere('phone','like','%'.trim($filter).'%');
        })->orderBy('create_time','desc')->select('id','nickname','phone','create_time','sex')->paginate(0,['*'],'page',1)->toArray();
        foreach($users['data'] as &$val){
            $val['sex_msg'] = User::getSex($val['sex']);
        }
        layTableApi($users['total'],$users['data']);
    }

}
